package com.atguigu.edupublisher.controller;

import com.atguigu.edupublisher.beans.TestQuestionAmount;
import com.atguigu.edupublisher.service.TestStatsService;
import com.atguigu.edupublisher.utils.DateFormatUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


import java.util.List;

@RestController
public class TestStatsController {
    @Autowired
    private TestStatsService testStatsService;
    @RequestMapping("/question")//拦截请求，将请求交给标注的方法进行处理。
    public String getQusetionAmount(@RequestParam(value = "date", defaultValue = "0") Integer date) {
        if (date == 0) {
            date = DateFormatUtil.now();
        }

        List<TestQuestionAmount> questionAmountList = testStatsService.getQuestionAmount(date);
        if (questionAmountList == null || questionAmountList.size() == 0) {
            return "";
        }
        StringBuilder dataSet = new StringBuilder("[");
        for (int i = 0; i < questionAmountList.size(); i++) {
            TestQuestionAmount testQuestionAmount = questionAmountList.get(i);
            String questionId = testQuestionAmount.getQuestionId();
            Integer questionRightCount = testQuestionAmount.getQuestionRightCount();
            Integer questionCount = testQuestionAmount.getQuestionCount();

            Integer questionRightUniqueUserCount = testQuestionAmount.getQuestionRightUniqueUserCount();
            Integer questionUniqueUserCount = testQuestionAmount.getQuestionUniqueUserCount();
            dataSet.append("{\n" +
                    "        \"questionId\": \"" + questionId + "\",\n" +
                    "        \"questionRightCount\": \"" + questionRightCount + "\",\n" +
                    "        \"questionCount\": \"" + questionCount + "\",\n" +
                    "        \"questionRightUniqueUserCount\": \"" + questionRightUniqueUserCount + "\",\n" +
                    "        \"questionUniqueUserCount\": \"" + questionUniqueUserCount + "\"\n" +
                    "      }");
            if (i < questionAmountList.size() - 1) {
                dataSet.append(",");
            } else {
                dataSet.append("]");
            }
        }

        return "{\n" +
                "  \"status\": 0,\n" +
                "  \"data\": {\n" +
                "    \"columns\": [\n" +
                "      {\n" +
                "        \"name\": \"题目\",\n" +
                "        \"id\": \"questionId\"\n" +
                "      },\n" +
                "      {\n" +
                "        \"name\": \"正确答题次数\",\n" +
                "        \"id\": \"questionRightCount\"\n" +
                "      },\n" +
                "      {\n" +
                "        \"name\": \"答题次数\",\n" +
                "        \"id\": \"questionCount\"\n" +
                "      },\n" +
                "      {\n" +
                "        \"name\": \"正确答题独立用户数\",\n" +
                "        \"id\": \"questionRightUniqueUserCount\"\n" +
                "      }\n" +
                "    ],\n" +
                "    \"rows\": " + dataSet + "\n" +
                "  }\n" +
                "}";
    }}